<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>obj</title>
<!--    模拟Java的map方法-->
    <script type="text/javascript">
        function Map () {
            var obj = {};//空的对象容器，承装键值对
            // put 方法
            this.put = function (key,value) {
                obj[key] = value;
            }

            // size方法，获得map容器的个数
            this.size = function () {
                var count = 0;
                for (var attr in obj){
                    count++;
                }
                return count;
            }

            // get方法，根据key获得value
            this.get = function (key) {
                if(obj[key] || obj[key] === false || obj[key] === 0){
                    return obj[key];
                }else {
                    return null;
                }
            }

            // remove方法，删除
            this.remove = function (key) {
                if(obj[key] || obj[key] === false || obj[key] === 0){
                    delete obj[key];
                }
            }
            
            // eachMap方法，遍历map元素
            this.eachMap = function (fn) {
                for (var attr in obj){
                    fn(attr,obj[attr]);
                }
            }
        }


        var m = new Map();
        m.put('01','abc');
        m.put('02','def');
        m.put('03',true);
        m.put('04',new Date());
        alert("m.size():"+m.size())
        m.remove('02');
        alert("m.get():"+m.get('02'))
        m.eachMap(function (key,value) {
            alert(key+" : "+value);
        })

    </script>

</head>
<body>

</body>
</html>